//
//  UIView+Category.m
//  LCFN-Buyers
//
//  Created by lcfn on 2018/3/30.
//  Copyright © 2018年 royce. All rights reserved.
//

#import "UIView+Category.h"

@implementation UIView (Category)
+ (instancetype)loadInstanceFromNib
{
    return [self loadInstanceFromNibWithName:NSStringFromClass([self class]) owner:nil bundle:[NSBundle mainBundle]];
}
+ (instancetype)loadInstanceFromNibWithName:(NSString *)nibName owner:(id)owner bundle:(NSBundle *)bundle
{
    UIView *result = nil;
    NSArray* elements = [bundle loadNibNamed:nibName owner:owner options:nil];
    for (id object in elements)
    {
        if ([object isKindOfClass:[self class]])
        {
            result = object;
            break;
        }
    }
    return result;
}
+ (void)setLayerRadius:(UIView *)view radiusWidth:(NSInteger)width
{
    view.layer.cornerRadius = width;
    view.layer.masksToBounds = YES;
}
+ (void)setLayerShadowView:(UIView *)view color:(UIColor *)color shadowRadius:(NSInteger)radius
{
    if (color != nil) {
        view.layer.shadowColor = color.CGColor;
    }else
    {
        view.layer.shadowColor = [UIColor grayColor].CGColor;
    }
    view.layer.shadowColor = color.CGColor;
    view.layer.shadowOffset = CGSizeMake(0, 0);//设置阴影的偏移量
    view.layer.shadowOpacity = 0.7;//设置阴影的透明度
    view.layer.shadowRadius = radius;
    view.clipsToBounds = NO;
}
+ (void)setLayerMainView:(UIView *)mainView subView:(UIView *)subView radiusWidth:(NSInteger)width shadowRadius:(NSInteger)radius
{
    [self  setLayerMainView:mainView subView:subView radiusWidth:width shadowRadius:radius shadowSize:CGSizeMake(0, 0) shadowColor:[UIColor grayColor]];
}
+ (void)setLayerMainView:(UIView *)mainView subView:(UIView *)subView radiusWidth:(NSInteger)width shadowRadius:(NSInteger)radius shadowSize:(CGSize) size shadowColor:(UIColor *)color{
    subView.layer.cornerRadius = width;
    subView.layer.masksToBounds = YES;
    mainView.layer.shadowColor = color.CGColor;
    mainView.layer.shadowOffset = size;//设置阴影的偏移量
    mainView.layer.shadowOpacity = 0.7;//设置阴影的透明度
    mainView.layer.shadowRadius = radius;
    mainView.clipsToBounds = NO;
}

+ (void)setLayerViewShadow:(UIView *)view radiusWidth:(NSInteger)width borderColor:(UIColor *)color borderWidth:(CGFloat)widthBorder
{
    view.layer.cornerRadius = width;
    view.layer.masksToBounds = YES;
    view.layer.borderColor = color.CGColor;//边框颜色
    view.layer.borderWidth = widthBorder;//边框宽度
}
+(void)setLayerView:(UIView *)view topLeft:(BOOL)topLeft topRight:(BOOL)topRight bottomLeft:(BOOL)bottomLeft bottomRight:(BOOL)bottomRight borderWidth:(CGFloat)widthBorder
{
    if (topLeft == YES && topRight == YES && bottomLeft == NO && bottomRight == NO) {
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerTopRight cornerRadii:CGSizeMake(widthBorder, widthBorder)];
        CAShapeLayer *layer = [[CAShapeLayer alloc]init];
        layer.frame = view.bounds;
        layer.path = maskPath.CGPath;
        layer.masksToBounds = YES;
        view.layer.mask = layer;
    }else if (topLeft == NO && topRight == NO && bottomLeft == YES && bottomRight == YES)
    {
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(widthBorder, widthBorder)];
        CAShapeLayer *layer = [[CAShapeLayer alloc]init];
        layer.frame = view.bounds;
        layer.path = maskPath.CGPath;
        layer.masksToBounds = YES;
        view.layer.mask = layer;
    }else if (topLeft == YES && topRight == NO && bottomLeft == NO && bottomRight == YES)
    {
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:UIRectCornerTopLeft cornerRadii:CGSizeMake(widthBorder, widthBorder)];
        CAShapeLayer *layer = [[CAShapeLayer alloc]init];
        layer.frame = view.bounds;
        layer.path = maskPath.CGPath;
        layer.masksToBounds = YES;
        view.layer.mask = layer;
    }else if (topLeft == NO && topRight == YES && bottomLeft == NO && bottomRight == NO)
    {
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:UIRectCornerTopRight cornerRadii:CGSizeMake(widthBorder, widthBorder)];
        CAShapeLayer *layer = [[CAShapeLayer alloc]init];
        layer.frame = view.bounds;
        layer.path = maskPath.CGPath;
        layer.masksToBounds = YES;
        view.layer.mask = layer;
    }else if (topLeft == NO && topRight == NO && bottomLeft == YES && bottomRight == NO)
    {
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:UIRectCornerBottomLeft cornerRadii:CGSizeMake(widthBorder, widthBorder)];
        CAShapeLayer *layer = [[CAShapeLayer alloc]init];
        layer.frame = view.bounds;
        layer.path = maskPath.CGPath;
        layer.masksToBounds = YES;
        view.layer.mask = layer;
    }else if (topLeft == NO && topRight == NO && bottomLeft == NO && bottomRight == YES)
    {
        UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:view.bounds byRoundingCorners:UIRectCornerBottomRight cornerRadii:CGSizeMake(widthBorder, widthBorder)];
        CAShapeLayer *layer = [[CAShapeLayer alloc]init];
        layer.frame = view.bounds;
        layer.path = maskPath.CGPath;
        layer.masksToBounds = YES;
        view.layer.mask = layer;
    }
}
+ (void)drawLineOfDashByCAShapeLayer:(UIView *)lineView lineLength:(int)lineLength lineSpacing:(int)lineSpacing lineColor:(UIColor *)lineColor lineDirection:(BOOL)isHorizonal {
    
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    
    [shapeLayer setBounds:lineView.bounds];
    
    if (isHorizonal) {
        
        [shapeLayer setPosition:CGPointMake(CGRectGetWidth(lineView.frame) / 2, CGRectGetHeight(lineView.frame))];
        
    } else{
        [shapeLayer setPosition:CGPointMake(CGRectGetWidth(lineView.frame) / 2, CGRectGetHeight(lineView.frame)/2)];
    }
    
    [shapeLayer setFillColor:[UIColor clearColor].CGColor];
    //  设置虚线颜色为blackColor
    [shapeLayer setStrokeColor:lineColor.CGColor];
    //  设置虚线宽度
    if (isHorizonal) {
        [shapeLayer setLineWidth:CGRectGetHeight(lineView.frame)];
    } else {
        
        [shapeLayer setLineWidth:CGRectGetWidth(lineView.frame)];
    }
    [shapeLayer setLineJoin:kCALineJoinRound];
    //  设置线宽，线间距
    [shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:lineLength], [NSNumber numberWithInt:lineSpacing], nil]];
    //  设置路径
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, 0, 0);
    
    if (isHorizonal) {
        CGPathAddLineToPoint(path, NULL,CGRectGetWidth(lineView.frame), 0);
    } else {
        CGPathAddLineToPoint(path, NULL, 0, CGRectGetHeight(lineView.frame));
    }
    
    [shapeLayer setPath:path];
    CGPathRelease(path);
    //  把绘制好的虚线添加上来
    [lineView.layer addSublayer:shapeLayer];
}
+ (void)drawBorderOfDashByCAShapeLayer:(UIView *)lineView cornerRadius:(CGFloat)radius lineWidth:(CGFloat)lineWidth lineColor:(UIColor *)lineColor{
    CAShapeLayer *border = [CAShapeLayer layer];
    
    //虚线的颜色
    border.strokeColor = lineColor.CGColor;
    //填充的颜色
    border.fillColor = [UIColor clearColor].CGColor;
    
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:lineView.bounds cornerRadius:radius];
    
    //设置路径
    border.path = path.CGPath;
    
    border.frame = lineView.bounds;
    //虚线的宽度
    border.lineWidth = lineWidth;
    
    
    //设置线条的样式
    //    border.lineCap = @"square";
    //虚线的间隔
    border.lineDashPattern = @[@2, @2];
    
    lineView.layer.cornerRadius = radius;
    lineView.layer.masksToBounds = YES;
    
    [lineView.layer addSublayer:border];
}

/**
 *  下拉刷新上啦加载
 */
- (void)setupRefresh:(UITableView *)tableView
{
    // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadNewData方法）
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRereshing)];
    // 设置自动切换透明度(在导航栏下面自动隐藏)
    header.automaticallyChangeAlpha = YES;
    // 隐藏时间
    header.lastUpdatedTimeLabel.hidden = NO;
    // 设置header
    tableView.mj_header = header;
    
    tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRereshing)];
    // 设置了底部inset
    tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    // 忽略掉底部inset
    tableView.mj_footer.ignoredScrollViewContentInsetBottom = 0;
}
//下拉刷新
- (void)setUpHeaderRefresh:(UITableView *)tableView
{
    // 设置回调（一旦进入刷新状态，就调用target的action，也就是调用self的loadNewData方法）
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRereshing)];
    // 设置自动切换透明度(在导航栏下面自动隐藏)
    header.automaticallyChangeAlpha = YES;
    // 隐藏时间
    header.lastUpdatedTimeLabel.hidden = NO;
    // 设置header
    tableView.mj_header = header;
    // 设置了底部inset
    tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
}
//上拉加载
- (void)setUpFooterRefresh:(UITableView *)tableView
{
    tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRereshing)];
    // 设置了底部inset
    tableView.contentInset = UIEdgeInsetsMake(0, 0, 0, 0);
    // 忽略掉底部inset
    tableView.mj_footer.ignoredScrollViewContentInsetBottom = 0;
}
- (void)headerRereshing
{
}
- (void)footerRereshing
{
}
- (void)headerEnd:(UITableView *)tableView
{
    [tableView.mj_header endRefreshing];
}
- (void)footerEnd:(UITableView *)tableView
{
    [tableView.mj_footer endRefreshing];
}
-(void)footerNoMoreData:(UITableView *)tableView{
    tableView.mj_footer.state = MJRefreshStateNoMoreData;
}


@end
